const mongoose = require("mongoose");
const Joi = require("joi");
const userSchema = new mongoose.Schema({
  email: {
    type: String,
    required: true,
    unique: true
  },
  nickName: {
    type: String,
    required: true,
    minlength: "2",
    maxlength: "20",
  },
  password: {
    type: String,
    required: true
  },
  introduction: {
    type: String,
  },
  registerTime: {
    type: Date,
    default: Date.now()
  }
});

const validateUser = user => {
  const Schema = Joi.object({
    email: Joi.string().email().required().error(new Error('邮箱不符合验证规则')),
    nickName: Joi.string().min(2).max(12).required().error(new Error('用户名不符合验证规则')),
    password: Joi.string().regex(/^\w{6,15}$/).required().error(new Error('密码格式不符合要求')),
    introduction: Joi.string().error(new Error('简介不符合验证规则'))
  })
  return Schema.validateAsync(user)
}



const User = mongoose.model("User", userSchema);
module.exports = { User, validateUser };
